<!-- 友链编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改友链':'添加友链'"
    :visible="visible"
    width="750px"
    :destroy-on-close="true"
    :lock-scroll="false"
    :close-on-click-modal="false"
    :before-close="confirmClose"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">
      <el-form-item label="系统头像："  prop="icon">
        <uploadImageFin :limit="1" v-model="form.icon"></uploadImageFin>
      </el-form-item>
      <el-row :gutter="15">
        <el-col :sm="12">
          <el-form-item label="头像名称:" prop="name">
            <el-input
              clearable
              :maxlength="20"
              v-model="form.name"
              placeholder="请输入头像名称"/>
          </el-form-item>

          <el-form-item label="排序号:" prop="sort">
            <el-input-number
              :min="0"
              v-model="form.sort"
              placeholder="请输入排序号"
              controls-position="right"
              class="ele-fluid ele-text-left"/>
          </el-form-item>
          <el-form-item label="友链状态:" prop="status">
            <el-radio-group
              v-model="form.status">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="2">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="备注:" style="margin-top: 20px">
        <el-input
          :rows="3"
          clearable
          type="textarea"
          :maxlength="200"
          v-model="form.desc"
          placeholder="请输入备注"/>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="confirmClose()">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImageFin from '@/components/uploadImageFin'

export default {
  name: 'LinkEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {uploadImageFin},
  data() {
    return {
      // 表单数据
      form: Object.assign({form: 1, status: 1, type: 1}, this.data),
      // 表单验证规则
      rules: {
        icon: [
          {required: true, message: '请上传系统头像', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入头像名称', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '请选择头像状态', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入排序号', trigger: 'blur'}
        ]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/BoxHotAvatar/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },

    confirmClose() {
      this.$confirm('确定要返回吗？', '温馨提示').then(() => {
        this.updateVisible(false)
        if(!this.isUpdate){
            this.form = {};
          }    
      }).catch(() => {

      });
    }
  }
}
</script>

<style scoped>
</style>
